import { BaseSample } from "./BaseSample";
import { OIMOHelper } from "../utils/OIMOHelper";

export class BasicTest extends BaseSample {

    name: string = "基础测试";

    private _sleepMat: Laya.BlinnPhongMaterial;
    private _awakeMat: Laya.BlinnPhongMaterial;

    _OnInit() {

        this._sleepMat = new Laya.BlinnPhongMaterial();
        this._sleepMat.albedoColor = new Laya.Vector4(0, 1, 0, 1);
        this._awakeMat = new Laya.BlinnPhongMaterial();
        this._awakeMat.albedoColor = new Laya.Vector4(1, 1, 0, 1);

        let panelMesh = Laya.PrimitiveMesh.createPlane(10, 10, 2, 2);
        let panelObj = new Laya.MeshSprite3D(panelMesh, "floor");
        panelObj.meshRenderer.material = new Laya.BlinnPhongMaterial();
        panelObj.meshRenderer.receiveShadow = true;
        this._s3d.addChild(panelObj);
        let rigConfig = new OIMO.RigidBodyConfig();
        rigConfig.position = new OIMO.Vec3(0, -0.5, 0);
        rigConfig.type = OIMO.RigidBodyType.STATIC;
        let shapeConfig = new OIMO.ShapeConfig();
        shapeConfig.geometry = new OIMO.BoxGeometry(new OIMO.Vec3(5, 0.5, 5));
        let shape = new OIMO.Shape(shapeConfig);
        let rig = new OIMO.RigidBody(rigConfig);
        rig.addShape(shape);
        this._world.addRigidBody(rig);

        this._AddBox();
    }

    private _AddBox() {
        let boxMesh = Laya.PrimitiveMesh.createBox(1, 1, 1);
        let boxObj = new Laya.MeshSprite3D(boxMesh, "box");
        boxObj.meshRenderer.sharedMaterial = this._sleepMat;
        boxObj.meshRenderer.castShadow = true;
        boxObj.meshRenderer.receiveShadow = true;
        this._s3d.addChild(boxObj);
        boxObj.transform.position = new Laya.Vector3(0, 10, 0);

        let rigConfig = new OIMO.RigidBodyConfig();
        rigConfig.position = new OIMO.Vec3(0, 10, 0);
        rigConfig.type = OIMO.RigidBodyType.DYNAMIC;
        let shapeConfig = new OIMO.ShapeConfig();
        shapeConfig.geometry = new OIMO.BoxGeometry(new OIMO.Vec3(0.5, 0.5, 0.5));
        let shape = new OIMO.Shape(shapeConfig);
        let rig = new OIMO.RigidBody(rigConfig);
        rig.addShape(shape);
        rig.userData = boxObj;

        this._world.addRigidBody(rig);
    }

    _OnLogicUpdate(dt: number) {
        this._world.step(dt);
        let worldRig = this._world.getRigidBodyList();
        while (worldRig != null) {
            if (worldRig.getType() == OIMO.RigidBodyType.STATIC) {
                worldRig = worldRig.getNext();
                continue;
            }
            OIMOHelper.SyncTrans(worldRig.userData, worldRig);
            worldRig = worldRig.getNext();
        }
    }

}